module ColHelper
  #根据资源得到分页后的具体资源数组,针对show的单个图片
  def get_resource_pagin(resource,type,pp_num)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    small_path = File.join($upload_path,resource.res_dir,type)
    
    src_arry = DataFile.traverse_dir(small_path, src_arry)   
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    if return_arry.size < pp_num.to_i
      pp_num = return_arry.size
    end
    if pp_num.to_i == 0
      pp_num = 2
    end
    @img_arry = return_arry.paginate(:per_page =>pp_num)
  end

  #大小图切换，定位图片的位置
  def get_resource_pagin_array(resource,type,to_page,pp_num)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    small_path = File.join($upload_path,resource.res_dir,type)

    src_arry = DataFile.traverse_dir(small_path, src_arry)
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    if return_arry.size < pp_num.to_i
      pp_num = return_arry.size      
    end
    if return_arry.size < pp_num.to_i * to_page.to_i
      if return_arry.size / pp_num.to_i == 0
        to_page = return_arry.size / pp_num.to_i
      else
        to_page = return_arry.size / pp_num.to_i + 1
      end
      
    end
    @img_arry = return_arry.paginate(:page =>to_page,:per_page =>pp_num)
  end

  #根据资源得到分页后的具体资源数组,针对专辑
  def get_resource_pagin_to_imgs(resource,type)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    small_path = File.join($upload_path,resource.res_dir,type)

    src_arry = DataFile.traverse_dir(small_path, src_arry)
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    @img_arry = return_arry.paginate(:page =>params[:page], :per_page =>$img_per_page)
  end


  #根据资源得到分页后的具体资源数组--- 将用户点击的数组放在第一位
  #为了得到更好的用户体验，把最先浏览到的图片放到最后，把用户点击的图片放到第一位
  def get_resource_index(resource,type,index)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    small_path = File.join($upload_path,resource.res_dir,type)

    src_arry = DataFile.traverse_dir(small_path, src_arry)
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    if index !=0
      temp1 = return_arry[-1]
      temp = return_arry[0]
      return_arry[0] = return_arry[index]
      return_arry[-1] = temp
      return_arry[index] = temp1
    end
    @img_arry = return_arry.paginate(:page =>params[:page], :per_page =>$img_per_page)
  end

  #根据资源得到分页后的具体资源数组,针对show的单个图片,对show这一页
  def get_resource_pagin_for_show(resource,type)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    small_path = File.join($upload_path,resource.res_dir,type)

    src_arry = DataFile.traverse_dir(small_path, src_arry)
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    @img_arry = return_arry.paginate(:page =>$img_per_page,:per_page =>$img_per_page)
  end

  #下载图片
  def get_resource_pagin_to_download(resource,type,index,pp_num,go_page)
    #得到的图片的数组
    src_arry = Array.new
    return_arry = Array.new
    pic_path = File.join($upload_path,resource.res_dir,$small)

    src_arry = DataFile.traverse_dir(pic_path, src_arry)
    src_arry.each  do |str|
      return_arry.push str.gsub($upload_path, $img_path)
    end
    img_arry = return_arry.paginate(:page =>go_page, :per_page =>pp_num)
    @view_img_path = img_arry[index]    
  end

  def format_filesize(url_path)
    size = File.size(url_path.gsub($img_path,$upload_path))
    size_str = "1"
    if size / 1024 > 1
      size = size / 1024
      size_str = size.to_s + "KB"      
    else
      size_str = size.to_s[0,3] + "KB"
    end
    return size_str
  end


  #  最新
  def get_res
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_page].to_i
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end

    unless params[:page_for].nil?
      to_page = params[:page_for]
      params[:page_for] = nil
    end
    @page_for = params[:page]

    host = request.domain
    le = $level_map[host.swapcase]
    @titles = le[:name]
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style.to_i == 2
      per_page = $res_per_page_img
    end
    to_page = params[:page]
    if to_page.nil?
      to_page = 1
    end

    @resouces = Resource.paginate :per_page => per_page, :page =>  to_page,
      :conditions =>  ["status=?  and level <= ? and created_at >=? ", 1, res_l,Time.now.strftime("%Y-%m-%d")],
      :order => 'created_at desc'
    t = Time.now
    i = 0
    while @resouces.size < 1
      i = i + 1
      @resouces = Resource.paginate :per_page => per_page, :page =>  to_page,
        :conditions =>  ["status=?  and level <= ? and created_at >=?", 1, res_l,(t - i*24*60*60).strftime("%Y-%m-%d")],
        :order => 'created_at desc'
    end
    if @resouces.size <= 1
      if !@resouces[0].nil?
        @titles = le[:name]
      else
        @titles = "暂无图片"
      end
    else
      @titles = le[:name]
    end
  end

  #分类
  def get_res_cat
    @oper_type = 1
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    @channels = Channel.find :all , :conditions => ['status = 1 and id = ?', $channel_cat], :order => 'orders asc'
    @titles = le[:name]
  end

  #新老用户判断
  def get_user_type
    @user_type = '0'
    user_cfg = UserCfg.find_last_by_sid params[:sid]
    unless user_cfg.nil?
      @user_type = user_cfg.user_type
    end
  end

  #输入中文
  def get_words str,length
    $KCODE="u"
    require "jcode"
    @r_str = ""
    index = 0
    str.each_char do |x|
      if index < length
        @r_str +=x
      end
      index +=1
    end
    if length < str.jlength
      @r_str +="..."
    end
    return @r_str
  end

  #游客昵称显示
  def get_nickname uc
    @nickname = "游客"
    if uc.user_type.to_i == 0 || uc.user_type.to_i == 1
      unless uc.user_sign.nil?
        @nickname = uc.user_sign
      end
    elsif uc.user_type.to_i == 2
      if !uc.nickname.nil?
        @nickname = uc.nickname
      elsif !uc.qq.nil?
        @nickname = uc.qq
      elsif !uc.user_sign.nil?
        @nickname = uc.user_sign
      end     
    else
      @nickname = "游客"
    end
    return @nickname
  end
end
